<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app;

use app\massage\model\Admin;
use think\App;

abstract class AdminRest extends BaseController
{
    public $_header = [];
    public $_token;
    public $_lang = "zh-cn";
    public $_role = "guest";
    public $_user;
    public $_uniacid = "666";
    protected $middleware = ["app\\middleware\\AppInit"];
    public $_is_weiqin = false;
    protected $card_auth_version = 0;
    protected $admin_arr = [];
    protected $card_auth_card = 0;

/*    public function __construct()
    {
        parent::__construct();
        $this->_token = $this->request->header("token");
        $_user = getUserForToken($this->_token);
        if (!$_user) {
            $this->errorMsg("登录信息失效", 401);
        }
        $admin = new Admin();
        $this->_user = array_merge($_user, $admin->checkAuthData($_user));
    }*/


    public function __construct(App $app)
    {
        parent::__construct($app);
        //获取token 通过header获取token,如果不存在,则从param中获取。
        if (!empty($this->_param['token'])) {
            $this->_header['token'] = $this->_param['token'];
        }
        if (empty($this->_header['token'])) {
            echo json_encode(['code' => 401, 'error' => '请重新登录!']);
            exit;
        }
        //获取token
        $this->_token = $this->_header['token'];
        //语言
        if (isset($this->_header['lang'])) $this->_lang = $this->_header['lang'];
        //获取用户信息
        $this->_user = getUserForToken($this->_token);
        if ($this->_user == null) {
            echo json_encode(['code' => 401, 'error' => '请登录系统!']);
            exit;
        }

        setUserForToken($this->_token, $this->_user);
        $this->_uniacid = !empty($this->_user) && isset($this->_user['uniacid']) ? $this->_user['uniacid'] : 2;
        $admin_model = new \app\massage\model\Admin();
        $this->_user = $admin_model->dataInfo(['id' => $this->_user['id']]);
        if ($this->_user['is_admin'] != 1 && $this->_user['status'] < 0) {
            echo json_encode(['code' => 401, 'error' => '请登录系统!']);
            exit;
        }
        $this->admin_arr = $admin_model->getAdminId($this->_user);
        if ($this->_user['is_admin'] == 3) {
            $user_info = $admin_model->dataInfo(['id' => $this->_user['admin_id']]);
            $this->_user['true_is_admin'] = $this->_user['is_admin'];
            $this->_user['is_admin'] = $user_info['is_admin'];
            $this->_user['phone_encryption'] = $user_info['phone_encryption'];
            $this->_user['login_auth_phone'] = $user_info['login_auth_phone'];
        } else {
            $this->_user['admin_id'] = $this->_user['id'];
        }
        if (in_array($this->_user['is_admin'], [1, 2])) {
            $this->_user['phone_encryption'] = 1;
        }
        $admin_model->initAgentName();
    }

    public function success($data, $code = 200, $obj_id = NULL)
    {
        $result["data"] = $data;
        $result["code"] = $code;
        $result["sign"] = NULL;
        if (!empty($this->_token)) {
            $result["sign"] = createSimpleSign($this->_token, is_string($data) ? $data : json_encode($data));
        }
        $this->controlActionLog($obj_id);
        return $this->response($result, "json", $code);
    }

    public function controlActionLog()
    {
    }

    public function shareChangeData($input)
    {
        $arr = ["/admin/admin/config/clear", "/massage/admin/AdminOrder/noLookCount", "/massage/admin/AdminSetting/getSaasAuth"];
        if (!empty($input["s"]) && in_array($input["s"], $arr)) {
            return false;
        }
        return true;
    }

    public function _empty($method)
    {
        if (method_exists($this, $method . "_" . $this->method . "_" . $this->type)) {
            $fun = $method . "_" . $this->method . "_" . $this->type;
        } elseif ($this->method == $this->restDefaultMethod && method_exists($this, $method . "_" . $this->type)) {
            $fun = $method . "_" . $this->type;
        } elseif ($this->type == $this->restDefaultType && method_exists($this, $method . "_" . $this->method)) {
            $fun = $method . "_" . $this->method;
        }
        if (isset($fun)) {
            return \think\App::invokeMethod([$this, $fun]);
        }
        throw new \Exception("error action :" . $method);
    }

    public function payConfig()
    {
    }
}

